package com.hyyt.shopjsp.rating.buyersStrategyAndRecord.controller;

import com.hyyt.shopjsp.rating.buyersStrategyAndRecord.pojo.RatingBuyersStrategy;
import com.hyyt.shopjsp.rating.buyersStrategyAndRecord.service.IRatingBuyersStrategyService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hyyt.shopjsp.common.controller.BaseController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/buyersStrategy")
public class RatingBuyersStrategyController extends BaseController {
	private Logger log = Logger.getLogger(RatingBuyersStrategyController.class);
	private String basePath = "/back/rating/buyersStrategyAndRecord/";

	@Resource
	private IRatingBuyersStrategyService ratingBuyersStrategyService;//数据字典service接口

	/**
	 * 进入买家等级策略
	 *
	 * @return 返回到指定页面
	 */
	@RequestMapping("gotoBuyersStrategyPage")
	public String gotoBuyersStrategyPage() {
		return basePath + "listBuyersStrategy";
	}


	/**
	 * 查询所有买家等级策略
	 *
	 * @param page
	 * @param response
	 */
	@RequestMapping(value = "listBuyersStrategy", method = RequestMethod.POST)
	@ResponseBody
	public void listBuyersStrategy(Page page, String buyersLevel ,HttpServletResponse response) {
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(buyersLevel)) {//是否检索类型名称
			param.put("buyersLevel", buyersLevel.trim());
		}
		param.put("sortColumns", "buyersLevelId desc");
		int totalRecordCount = ratingBuyersStrategyService.countRatingBuyersStrategy(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<RatingBuyersStrategy> list = ratingBuyersStrategyService.queryRatingBuyersStrategy(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 根据等级策略id获得对象
	 *
	 * @param buyersLevelId
	 */
	@RequestMapping(value = "getBuyersStrategyInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getBuyersStrategyInfo(Integer buyersLevelId) {
		if (Utils.objectIsNotEmpty(buyersLevelId)) {
			RatingBuyersStrategy ratingBuyersStrategy = ratingBuyersStrategyService.loadRatingBuyersStrategy(buyersLevelId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(ratingBuyersStrategy));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 保存或者修改等级策略
	 *
	 * @param ratingBuyersStrategy
	 * @return
	 */
	@RequestMapping(value = "saveOrUpdateRatingBuyersStrategy", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateRatingBuyersStrategy(RatingBuyersStrategy ratingBuyersStrategy) {
		if (Utils.objectIsNotEmpty(ratingBuyersStrategy)) {
			if (Utils.objectIsEmpty(ratingBuyersStrategy.getBuyersLevelId())) {
				ratingBuyersStrategyService.saveRatingBuyersStrategy(ratingBuyersStrategy);
			} else {
				ratingBuyersStrategyService.updateRatingBuyersStrategy(ratingBuyersStrategy);
			}
		}
		return ResultMsg.create(true);
	}

	/**
	 * 根据传入的等级策略id删除
	 *
	 * @param buyersLevelId
	 */
	@RequestMapping(value = "deleteById", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteById(Integer buyersLevelId) {
		if (Utils.objectIsNotEmpty(buyersLevelId)) {
			ratingBuyersStrategyService.deleteRatingBuyersStrategy(buyersLevelId);
		}
		return ResultMsg.create(true);
	}

}